package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.GateMapper;
import com.zhijian.warehouse.manage.entity.po.Gate;
import com.zhijian.warehouse.manage.entity.vo.request.GateSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.GateSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.GateVo;
import com.zhijian.warehouse.manage.service.GateService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 通道门业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-11 10:35:59
 */
@Slf4j
@Service
public class GateServiceImpl implements GateService {

    @Autowired
    private GateMapper mapper;

    @Override
    public Gate selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public GateVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(Gate gate) {
        Date now = new Date();
        gate.setCreateTime(now);
        gate.setUpdateTime(now);
        return mapper.insert(gate) > 0 ? gate.getId() : 0L;
    }

    @Override
    public Long insertSelective(Gate gate) {
        Date now = new Date();
        gate.setCreateTime(now);
        gate.setUpdateTime(now);
        return mapper.insertSelective(gate) > 0 ? gate.getId() : 0L;
    }

    @Override
    public boolean update(Gate gate) {
        Date now = new Date();
        gate.setUpdateTime(now);
        return mapper.updateByPrimaryKey(gate) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Gate record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(GateSaveOrUpdateVo saveOrUpdateVo) {
        Integer count = this.count(GateSearchVo.builder()
                .ipAddress(saveOrUpdateVo.getIpAddress())
                .build());
        AssertCommonUtil.check(count > 0, "ip地址重复");
        Gate gate = new Gate();
        BeanUtils.copyProperties(saveOrUpdateVo, gate);
        return this.insert(gate) != 0;
    }

    @Override
    public boolean modify(GateSaveOrUpdateVo saveOrUpdateVo) {
        Gate old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        Integer count = this.count(GateSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .ipAddress(saveOrUpdateVo.getIpAddress())
                .build());
        AssertCommonUtil.check(count > 0, "ip地址重复");
        old.setIpAddress(saveOrUpdateVo.getIpAddress());
        old.setNote(saveOrUpdateVo.getNote());
        old.setLocation(saveOrUpdateVo.getLocation());
        old.setUpdateUserId(saveOrUpdateVo.getUpdateUserId());
        old.setUpdateUserName(saveOrUpdateVo.getUpdateUserName());
        return this.update(old);
    }

    @Override
    public GateVo selectOne(GateSearchVo gateSearchVo) {
        return mapper.selectOne(gateSearchVo);
    }

    @Override
    public Integer count(GateSearchVo gateSearchVo) {
        return mapper.count(gateSearchVo);
    }

    @Override
    public List<GateVo> selectList(GateSearchVo gateSearchVo) {
        return mapper.selectList(gateSearchVo);
    }

    @Override
    public Page<GateVo> selectPage(GateSearchVo gateSearchVo) {
        AssertCommonUtil.check(Objects.isNull(gateSearchVo.getPageNum()) || Objects.isNull(gateSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(gateSearchVo);
        //分页
        Page<GateVo> page = new Page<>(true, count, gateSearchVo.getPageNum(), gateSearchVo.getPageSize());
        gateSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<GateVo> responseVos = this.selectList(gateSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Gate> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(GateSearchVo gateSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(gateSearchVo);
    }

}